package com.zlcx.tz.live.web;

import com.alibaba.fastjson.JSONObject;
import com.zlcx.tz.live.common.annotation.AutoLog;
import com.zlcx.tz.live.exception.BusinessException;
import com.zlcx.tz.live.exception.CheckRunException;
import com.zlcx.tz.live.service.impl.FFMPEGService;
import com.zlcx.tz.live.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @AUTHOR xhl
 * @CREATE 2019-06-18 18:28
 **/
@Controller
@Slf4j
@RequestMapping("/liveVideo")
@Api(tags = {"录制直播视频"}, description = "录制直播抽帧视频")
public class LiveVideoController  extends BaseController{
    @Autowired
    FFMPEGService ffmpegService;
    @Autowired
    RedisTemplate redisTemplate;
    @Value("${ffmpeg.path.video}")
    String videoPath;

//    @ApiOperation(value = "录制直播视频调试", notes = "录制直播视频调试", response = AgoraAcountVO.class, responseContainer = "Map")
//    @GetMapping(value = "/createVideo")
//    public ResponseEntity<ResultVO> current(@ApiParam(value = "uid", required = true)Long uid,
//                                            @ApiParam(value = "uid", required = true)Long roomId
//                                                /*@RequestHeader(required = false) String authorization*/) {
//
//        try {
//
//            String recordingPicDir = RedisKeyConst.getRecordingPicDirKey(uid,roomId);
//            String imgsPath = (String)redisTemplate.opsForValue().get(recordingPicDir);
//            if(StringUtils.isEmpty(imgsPath)){
//                log.info("createVideo not found dir data,uid={},channelId={} ",uid,roomId);
//                throw  new BusinessException("没有数据");
//            }
//
//            String fileName = ffmpegService.createVideo(uid,roomId,imgsPath,videoPath);
//            return ResponseEntity.ok(successState(videoPath+fileName));
//
//        } catch (BusinessException e) {
//            throw e;
//        } catch (Exception e) {
//            log.error("录制直播视频失败，e={}", e);
//            throw new CheckRunException("录制直播视频失败!", e);
//        }
//
//    }

    @AutoLog
    @PreAuthorize("isAuthenticated()")
    @ApiOperation(value = "获取连麦录制视频", notes = "获取连麦录制视频")
    @GetMapping(value = "/getRecordVideo/{roomId}/{userId}")
    public ResponseEntity<ResultVO> getRecordVideo(@ApiParam(value = "token", required = true) @RequestHeader String authorization,
                                             @ApiParam(value = "房间id", required = true) @PathVariable Long roomId,
                                             @ApiParam(value = "用户id", required = true) @PathVariable Long userId) {

        try {


            String videoUrl = ffmpegService.getRecordVideo(roomId, userId);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("videoUrl",videoUrl);
            return ResponseEntity.ok(successState(jsonObject));

        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("获取连麦录制视频失败，roomId={},userId={}", roomId, userId, e);
            throw new CheckRunException("获取连麦录制视频失败!", e);
        }

    }












}
